<?php

class EventsController extends Controller_Action
{
    /**
     * @var Model\Users
     */
    private $mUsers     = NULL;
    /**
     * @var Model\Events
     */
    private $mEvents    = NULL;
    /**
     * @var Model\Events\filterInput
     */
    private $mFInput    = NULL;
    
    public function init()
    {
        parent::init();
        
        // i18n
        \Helper\loadI18n('events', 'main');
        
        // models
        $this->mUsers = new Model\Users();
        
        \Helper\loadModel('events', 'events');
        $this->mEvents = new Model\Events();
        
        \Helper\loadModel('events', 'filterInput');
        $this->mFInput  = new Model\Events\filterInput();
    }
    
    public function indexAction($timeStart = NULL,
                                $timeEnd = NULL,
                                $userName = NULL)
    {   
        //$this->_helper->viewRenderer->setNoRender();
        //$this->_helper->redirector('login');
        
        /*if (empty($userName) &&
            $this->userName)
        {
            //$this->userName
        }
        elseif ( ! empty($userName) &&
                $this->mUsers->existsUserUserName($userName))
        {
            //$userName
        }
        else
        {
            // non existed user (userName)
        }*/
        
        $this->view->yearSlider = $this->mEvents->getYearInterval($this->userId);
        $this->view->eventTypes = $this->mEvents->getTypes();
        
        if ($this->mEvents->helperIsGetParamDate($timeStart) &&
            $this->mEvents->helperIsGetParamDate($timeEnd) &&
            \strtotime($timeStart) < \strtotime($timeEnd)) {
            $this->view->getParamTimeStart  = $timeStart;
            $this->view->getParamTimeEnd    = $timeEnd;
        }
    }
    
    public function ajaxGetEventsAction($timeStart, $timeEnd) {
        $events =
        $this->mEvents->getEventsByDTimeInterval(
            $this->userId, $timeStart, $timeEnd, array('id', 'name'));
        echo \json_encode($events);
    }
    
    public function ajaxPostAddNewEventAction() {
        $result = array('status'    => 'OK',
                        'msgs'      => array(   'bsns' => array(),
                                                'form' => array()));
        
        // Form validate
        $errorMsgs  = array();
        $filtInp    = $this->mFInput->addNewEvent();
        if ( ! $this->filterInput(  $filtInp->filters,
                                    $filtInp->validators,
                                    $_POST, FALSE, $errorMsgs)) {
            $result['status']       = 'ERROR';
            $result['msgs']['form'] = $errorMsgs;
            
            echo \json_encode($result);
            return FALSE;
        }
        // Bsns validate
        $timeStart  = new \Zend_Date($this->inPost['timeStart']);
        $timeEnd    = new \Zend_Date($this->inPost['timeEnd']);
        
        $existEvent = $this->mEvents->existsEvent(  $this->userId, 
                                                    $timeStart->getTimestamp(),
                                                    $timeEnd->getTimestamp());
        if ($timeEnd < $timeStart ||
            $timeStart->getTimestamp() > $_SERVER['REQUEST_TIME'] ||
            $timeEnd->getTimestamp() > $_SERVER['REQUEST_TIME']) {
            $existEvent = TRUE;
        }
        
        if ($existEvent) {
            $result['status']           = 'ERROR';
            $result['msgs']['bsns'][]   = \ZReg::get('i18n')->get('reservedEventDate');
            
            echo \json_encode($result);
            return FALSE;
        }
        
        // Insert
        $this->mEvents->insert( $this->userId,
                                $this->inPost['name'],
                                $this->inPost['type'],
                                $timeStart->getTimestamp(),
                                $timeEnd->getTimestamp());
        
        echo \json_encode($result);
        return TRUE;
        
        // csak a múltra lehessen felvenni eseményt!!!
        // ha nincs hiba, akkor oda kell ugrani az időben, ahol ez az event van!
    }
}